<?php
require_once  __DIR__ . '/includes/authenticate.php';
include_once __DIR__ . '/db_bb_connect.php';
?>

<style>
  input[type='number'] {
    -moz-appearance:textfield;
}
</style>

<div id="subcontent">
<form method="post" action="" class="hidden-print"> 
  <center><h3>Add/Edit Game Results</h3></center>
	<div class="form-group">
	  <div class="col-md-offset-3 col-md-6">        
	<select name="season" class="form-control" id="select1">

<?php

if (isset($_POST['season'])) {
	$setseason = $_POST['season'];

	echo "<option id=\"selected-season\" value=\"$setseason\" selected=\"selected\">$setseason is selected</option>";
} else {
	echo "<option id=\"selected-season\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a season</option>";
}
?>

<?php

$sql = "SELECT DISTINCT season 
		FROM games
		ORDER BY season";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
				$season = $row['season'];
				echo "<option value=\"$season\">$season</option>";
};
?>

	</select>
	</div>
	</div>

<?php

if (isset($_POST['season'])) {
	$setseason = $_POST['season'];




?>
<br> <br>
<div class="form-group">
	  <div class="col-md-offset-3 col-md-6">        

<select name="selectedgame" class="form-control" id="select2">

<?php 

if (isset($_POST['season']) && isset($_POST['selectedgame'])) {

	$setseason = $_POST['season'];
	$selectedgame = $_POST['selectedgame'];

	echo "<option id=\"selected-game\" value=\"$selectedgame\" selected=\"selected\">$setseason game $selectedgame selected</option>";         

} else {
	echo "<option id=\"selected-game\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a game</option>";
}


$sql = "SELECT gamenumber, date, opponent, location
FROM games
WHERE season = '$setseason'
ORDER BY date DESC";

$result=$db->query($sql);

while($row=$result->fetch_assoc()){ 
    $gamenumber = $row['gamenumber'];
    $date = $row['date'];
    $opponent = $row['opponent'];
    $location = $row['location'];

  echo "<option value='$gamenumber'>Game $gamenumber $date ($opponent $location)  </option>";
}

?>
  </select>

</div>
</div>

<?php } 

if (isset($_POST['season']) && isset($_POST['selectedgame'])) {
	$setseason = $_POST['season'];
	$selectedgame = $_POST['selectedgame'];

	$sql4 = "SELECT * FROM games 
	WHERE season = '$setseason' 
	AND gamenumber = '$selectedgame'";

	$result4=$db->query($sql4);
	$row4=$result4->fetch_assoc();

	$gamenumber = $row4['gamenumber'];
	$date = $row4['date'];
	$opponent = $row4['opponent'];
	$location = $row4['location'];

	$score_exc_tot = $row4['score_exc_tot'];
	$score_opp_tot = $row4['score_opp_tot'];

	$score_exc_q1 = $row4['score_exc_q1'];
	$score_exc_q2 = $row4['score_exc_q2'];
	$score_exc_q3 = $row4['score_exc_q3'];
	$score_exc_q4 = $row4['score_exc_q4'];

	$score_opp_q1 = $row4['score_opp_q1'];
	$score_opp_q2 = $row4['score_opp_q2'];
	$score_opp_q3 = $row4['score_opp_q3'];
	$score_opp_q4 = $row4['score_opp_q4'];

	echo "<br><br><center><h4>Edit $setseason game $selectedgame $opponent $location </h4> </center><br>"; 

?>


<div class="form-group">
  <label for="usr">Coachteam total</label>
  <input type="number" class="form-control" placeholder="Coachteam total" name="exctot" value="<?= $score_exc_tot; ?>">
</div>

<div class="form-group">
  <label for="usr">Opponent total</label>
  <input type="number" class="form-control" placeholder="Opponent total" name="opptot" value="<?= $score_opp_tot; ?>">
</div>

<div class="form-group">
  <label for="usr">Coachteam Q1</label>
  <input type="number" class="form-control" placeholder="Coachteam Q1" name="excq1" value="<?= $score_exc_q1; ?>">
</div>

<div class="form-group">
  <label for="usr">Opponent Q1</label>
  <input type="number" class="form-control" placeholder="Opponent Q1" name="oppq1" value="<?= $score_opp_q1; ?>">
</div>

<div class="form-group">
  <label for="usr">Coachteam Q2</label>
  <input type="number" class="form-control" placeholder="Coachteam Q2" name="excq2" value="<?= $score_exc_q2; ?>">
</div>

<div class="form-group">
  <label for="usr">Opponent Q2</label>
  <input type="number" class="form-control" placeholder="Opponent Q2" name="oppq2" value="<?= $score_opp_q2; ?>">
</div>

<div class="form-group">
  <label for="usr">Coachteam Q3</label>
  <input type="number" class="form-control" placeholder="Coachteam Q3" name="excq3" value="<?= $score_exc_q3; ?>">
</div>

<div class="form-group">
  <label for="usr">Opponent Q3</label>
  <input type="number" class="form-control" placeholder="Opponent Q3" name="oppq3" value="<?= $score_opp_q3; ?>">
</div>

<div class="form-group">
  <label for="usr">Coachteam Q4</label>
  <input type="number" class="form-control" placeholder="Coachteam Q4" name="excq4" value="<?= $score_exc_q4; ?>">
</div>

<div class="form-group">
  <label for="usr">Opponent Q4</label>
  <input type="number" class="form-control" placeholder="Opponent Q4" name="oppq4" value="<?= $score_opp_q4; ?>">
</div>

<button type="submit" class="btn btn-success btn-lg btn-block submitresultsbutton">Submit Results</button>

<br>

<?php } ?>



</form>

</div>


<script>

$("#select1").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
	  type: "POST",
	  url: "./admin_addeditgameresults.php",
	  data: postData,
	  success: function(data) {
	  	$("#subcontent").html(data);
  	  }
  }); 
  e.preventDefault();
});

$("#select2").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
	  type: "POST",
	  url: "./admin_addeditgameresults.php",
	  data: postData,
	  success: function(data) {
	  	$("#subcontent").html(data);
  	  }
  }); 
  e.preventDefault();
});


$(".submitresultsbutton").on("click", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
	  type: "POST",
	  url: "./includes/handlers/admin_addeditgameresults_handler.php",
	  data: postData,
	  success: function(data) {
	  	$("#subcontent").html(data);
  	  },
  	  error: function (xhr, status, error) {
          $("#subcontent").html(xhr.responseText);
        }
  }); 
  e.preventDefault();
});

</script>